package com.tongji.housediy.model
{   	
	import com.tongji.housediy.events.UpdateFloorDataEvent;
	import com.tongji.housediy.utils.ConstVariableInventory;
	import com.tongji.housediy.utils.LoadAdmin;
	import com.tongji.housediy.vo.Goods;
	import com.tongji.housediy.vo.GoodsSort;
	import com.tongji.housediy.vo.Picture;
	import com.tongji.housediy.vo.RelateGoods;
	import com.tongji.housediy.vo.Size;
	
	import flash.display.BitmapData;
	import flash.display.Loader;
	import flash.events.Event;
	
	import mx.collections.ArrayCollection;
	
	import org.robotlegs.mvcs.Actor;
	
	
	/**
	 * 
	 * @author yanghanbing
	 * 2011/2/16
	 */
	[Bindable]
	public class GoodsModel extends Actor
	{
		public var doorArrayCollection:ArrayCollection;
		public var windowArrayCollection:ArrayCollection;
		public var floorArrayCollection:ArrayCollection;
		public var furnitureArrayCollection:ArrayCollection;
		public var wallArrayCollection:ArrayCollection;
		
		public var sizes:ArrayCollection = new ArrayCollection();
		public var models:ArrayCollection = new ArrayCollection();
		private var selectedIndexArray:Array;
		
		public var goodsName:String;
		public var goodsBrand:String;
		public var goodsType:String;
		public var picArray:ArrayCollection;
		public var colorArray:ArrayCollection;
		public var fittingArray:ArrayCollection;
		public var previewPicSrc:String;
		public var titleName:String;
		
		public var wallMap:BitmapData;
		public var skirtingMap:BitmapData;
		public var floorMap:BitmapData;
		
		public var doorBitMap:BitmapData;
		
		public var collectionArray:ArrayCollection;
		public var goodsCount:String;
		public function updateGoodsModel(goodsModel:ArrayCollection):void
		{
			if (goodsModel.length > 0)
			{
				switch ((goodsModel.getItemAt(0) as Goods).tag)
				{
					case "door":
						doorArrayCollection = goodsModel;
						break;
					case "window":
						windowArrayCollection = goodsModel;
						break;
					case "floor":
						floorArrayCollection = goodsModel;
						ConstVariableInventory.initialRoomGoods = floorArrayCollection.getItemAt(0) as Goods;
						ConstVariableInventory.initialFloorGoods = floorArrayCollection.getItemAt(0) as Goods;
						dispatch(new UpdateFloorDataEvent(UpdateFloorDataEvent.LOAD_ROOM_DATA, (floorArrayCollection.getItemAt(0) as Goods).dragPicSrc));
						dispatch(new UpdateFloorDataEvent(UpdateFloorDataEvent.LOAD_FLOOR_DATA, (floorArrayCollection.getItemAt(0) as Goods).dragPicSrc));
						break;
					case "furniture":
						furnitureArrayCollection = goodsModel;
						break;
					case "wall":
						wallArrayCollection = goodsModel;
						ConstVariableInventory.initialWallGoods = wallArrayCollection.getItemAt(0) as Goods;
						dispatch(new UpdateFloorDataEvent(UpdateFloorDataEvent.LOAD_WALL_DATA, (wallArrayCollection.getItemAt(0) as Goods).dragPicSrc));
						break;
				}
			}
			
		}
		
		public function filterArrayCollection(id:String, selectedIndexArray:Array):void
		{
			this.selectedIndexArray = selectedIndexArray;
			switch (id)
			{
				case "doorList":
					filterOneAC(doorArrayCollection);
					break;
				case "windowList":
					filterOneAC(windowArrayCollection);
					break;
				case "floorList1":
					filterOneAC(floorArrayCollection);
					break;
				case "furnitureList1":
					filterOneAC(furnitureArrayCollection);
					break;
				case "wallTypeList1":
					filterOneAC(wallArrayCollection);
					break;
			}
		}
		
		private function filterOneAC(data:ArrayCollection):void
		{
			if ((selectedIndexArray.length == 0) || (selectedIndexArray.length == 1 && selectedIndexArray[0] == 0))
				data.filterFunction = null;
			else
				data.filterFunction = filterDataFunction;
			
			data.refresh();
		}
		
		private function filterDataFunction(item:Object):Boolean
		{
			if (selectedIndexArray.indexOf(item.typeID.toString()) != -1)
				return true;
			else
				return false;
		}
		
		public function arrangeSizes(goods:Goods, titleName:String = ""):void
		{
			if (goods)
			{
				goodsName = goods.goodsName;
				goodsBrand = goods.brand.brandName;
				goodsType = goods.goodsType;
				picArray = goods.pictures;
				colorArray = goods.colors;
				fittingArray = goods.relateGoodsList;
				//sizes = goods.sizes;
				models = goods.models;
				previewPicSrc = (goods.pictures.getItemAt(0) as Picture).pictureSrc;
			}
			else
			{
				goodsName = "空";
				goodsBrand = "空";
				goodsType = "空";
				picArray = null;
				colorArray = null;
				fittingArray = null;
				sizes = null;
				previewPicSrc = "";
			}
			
			if (titleName != "")
				this.titleName = titleName;
		}
		
		public function arrangeMaps(wallMap:BitmapData, skirtingMap:BitmapData, floorMap:BitmapData):void
		{
			this.wallMap = wallMap;
			this.skirtingMap = skirtingMap;
			this.floorMap = floorMap;
		}
		
		public function getGoodsByFittingList(relatedGoods:RelateGoods):void
		{
			switch (relatedGoods.relateDictID)
			{
				case 1:
					arrangeSizes(getRelatedGoods(doorArrayCollection, relatedGoods.relateGoodsID));
					break;
				case 2:
					arrangeSizes(getRelatedGoods(windowArrayCollection, relatedGoods.relateGoodsID));
					break;
				case 3:
					arrangeSizes(getRelatedGoods(floorArrayCollection, relatedGoods.relateGoodsID));
					break;
				case 4:
					arrangeSizes(getRelatedGoods(furnitureArrayCollection, relatedGoods.relateGoodsID));
					break;
			}
		}
		
		private function getRelatedGoods(hostArray:ArrayCollection, id:int):Goods
		{
			for each (var goods:Goods in hostArray)
			{
				if (goods.goodsID == id)
					return goods;
			}
			return null;
		}
		
		public function rebulidGoods(goodsType:String, goodsID:int):Goods
		{
			var goods:Goods = new Goods();
			var i:int = 0;
			if(goodsType == "door")
			{
				for(i; i < doorArrayCollection.length; i++)
				{
					if((doorArrayCollection.getItemAt(i) as Goods).goodsID == goodsID)
					{
						goods = doorArrayCollection.getItemAt(i) as Goods;
					}
				}
			}
			else if(goodsType == "window" )
			{
				for(i; i < windowArrayCollection.length; i++)
				{
					if((windowArrayCollection.getItemAt(i) as Goods).goodsID == goodsID)
					{
						goods = windowArrayCollection.getItemAt(i) as Goods;
					}
				}
			}
			else if(goodsType == "floor")
			{
				for(i; i < floorArrayCollection.length; i++)
				{
					if((floorArrayCollection.getItemAt(i) as Goods).goodsID == goodsID)
					{
						goods = floorArrayCollection.getItemAt(i) as Goods;
					}
				}
			}
			else if(goodsType == "furniture")
			{
				for(i; i < furnitureArrayCollection.length; i++)
				{
					if((furnitureArrayCollection.getItemAt(i) as Goods).goodsID == goodsID)
					{
						goods = furnitureArrayCollection.getItemAt(i) as Goods;
					}
				}
			}
			else if(goodsType == "wall")
			{
				for(i; i < wallArrayCollection.length; i++)
				{
					if((wallArrayCollection.getItemAt(i) as Goods).goodsID == goodsID)
					{
						goods = wallArrayCollection.getItemAt(i) as Goods;
					}
				}
			}
			var loader:Loader = LoadAdmin.loadBitmapData(goods.dragPicSrc);
			loader.contentLoaderInfo.addEventListener(Event.COMPLETE, loadFloorCompleteHandler);
			return goods;
		}
		
		private function loadFloorCompleteHandler(event:Event):void
		{
			event.target.removeEventListener(Event.COMPLETE, loadFloorCompleteHandler);
			ConstVariableInventory.roomBitmapData = event.target.content.bitmapData;
		}
		
		/**
		 * 通过 goodsID 获取对应 Floor Goods
		 * @param goodsID
		 * @return 
		 * 
		 */		
		public function getFloorGoodsByID(goodsID:int):Goods
		{
			for each (var goods:Goods in floorArrayCollection)
			{
				if (goods.goodsID == goodsID)
					return goods;
			}
			return null;
		}
		
		/**
		 * 通过 goodsID 获取对应 Window Goods
		 * @param goodsID
		 * @return 
		 * 
		 */		
		public function getWindowGoodsByID(goodsID:int):Goods
		{
			for each (var goods:Goods in windowArrayCollection)
			{
				if (goods.goodsID == goodsID)
					return goods;
			}
			return null;
		}
		
		public function getWallGoodsByID(goodsID:int):Goods
		{
			for each (var goods:Goods in wallArrayCollection)
			{
				if (goods.goodsID == goodsID)
					return goods;
			}
			return null;
		}
		/**
		 * 通过 goodsID 获取对应 Furniture Goods
		 * @param goodsID
		 * @return 
		 * 
		 */		
		public function getFurnitureGoodsByID(goodsID:int):Goods
		{
			for each (var goods:Goods in furnitureArrayCollection)
			{
				if (goods.goodsID == goodsID)
					return goods;
			}
			return null;
		}
		
		/**
		 * 通过 goodsID 获取对应 Door Goods
		 * @param goodsID
		 * @return 
		 * 
		 */		
		public function getDoorGoodsByID(goodsID:int):Goods
		{
			for each (var goods:Goods in doorArrayCollection)
			{
				if (goods.goodsID == goodsID)
				{
					var loader:Loader = LoadAdmin.loadBitmapData(goods.dragPicSrc);
					loader.contentLoaderInfo.addEventListener(Event.COMPLETE, loadDoorCompleteHandler,true);
					loader.contentLoaderInfo.addEventListener(Event.COMPLETE, loadDoorCompleteHandler,false);
					return goods;
				}					
			}
			return null;
		}
		
		public function loadDoorCompleteHandler(event:Event):void
		{
			doorBitMap = event.target.content.bitmapData;
		}
		
		
		public function updateCollections(collectionArray:ArrayCollection):void
		{
			this.collectionArray = collectionArray;
			var goodsArray:ArrayCollection = new ArrayCollection();
			for each(var goodsSort:GoodsSort in collectionArray)
			{
				goodsArray.addAll(goodsSort.goodsList);
			}
			goodsCount = "共" + goodsArray.length + "个商品";;
		}
	}
}